#!/usr/bin/env sh

# Script to deploy certificates to conversant CDN

# The following variables exported from environment will be used.
# If not set then values previously saved in domain.conf file are used.
#
#
# export DEPLOY_CONVERSANT_CDN_ID=""  # access key id
# export DEPLOY_CONVERSANT_CDN_SECRET=""  # access key secret
# export DEPLOY_CONVERSANT_CDN_DOMAINS=""  # domains, used for SAN or wildcard certificate

########  Public functions #####################
conversant_cdn_m_deploy() {
  _cdomain="$1"
  _ckey="$2"
  _ccert="$3"
  _cca="$4"
  _cfullchain="$5"

  _debug _cdomain "$_cdomain"
  _debug _ckey "$_ckey"
  _debug _ccert "$_ccert"
  _debug _cca "$_cca"
  _debug _cfullchain "$_cfullchain"

  # whether use multi account.
  _getdeployconf DEPLOY_CONVERSANT_CDN_MULTI
  _debug2 DEPLOY_CONVERSANT_CDN_MULTI "$DEPLOY_CONVERSANT_CDN_MULTI"
  _savedeployconf DEPLOY_CONVERSANT_CDN_MULTI "$DEPLOY_CONVERSANT_CDN_MULTI"

  _conversant_cdn_m_deploy
  _err_code="$?"
  return $_err_code
}

_conversant_cdn_m_deploy() {
  _err_code=1
  # if not define DEPLOY_CONVERSANT_CDN_MULTI, break the loop
  if [ -z "$DEPLOY_CONVERSANT_CDN_MULTI" ]; then
      fix='null'
  else
      fix='null _0 _1 _2 _3 _4 _5 _6 _7 _8 _9'
  fi

  for suffix in `echo $fix`;do
    _info suffix $suffix

    if [ "$suffix" == "null" ];then
      suffix=''
    fi

    # access key id is required to access Ali api.
    _getdeployconf DEPLOY_CONVERSANT_CDN_ID$suffix
    eval TMP_VAL="\$DEPLOY_CONVERSANT_CDN_ID$suffix"
    _debug2 DEPLOY_CONVERSANT_CDN_ID$suffix "$TMP_VAL"
    if [ -z "$TMP_VAL" ]; then
      _info "DEPLOY_CONVERSANT_CDN_ID$suffix not defined."
      continue
    fi
    _savedeployconf DEPLOY_CONVERSANT_CDN_ID$suffix "$TMP_VAL"
    DEPLOY_CONVERSANT_CDN_ID="$TMP_VAL"

    # access key secret is required to access Ali api.
    _getdeployconf DEPLOY_CONVERSANT_CDN_SECRET$suffix
    eval TMP_VAL="\$DEPLOY_CONVERSANT_CDN_SECRET$suffix"
    _debug2 DEPLOY_CONVERSANT_CDN_SECRET$suffix "$TMP_VAL"
    if [ -z "$TMP_VAL" ]; then
      _info "DEPLOY_CONVERSANT_CDN_SECRET$suffix not defined."
      continue
    fi
    _savedeployconf DEPLOY_CONVERSANT_CDN_SECRET$suffix "$TMP_VAL"
    DEPLOY_CONVERSANT_CDN_SECRET="$TMP_VAL"

    # domain names is required when deploy SAN or wildcard cert.
    _getdeployconf DEPLOY_CONVERSANT_CDN_DOMAINS$suffix
    eval TMP_VAL="\$DEPLOY_CONVERSANT_CDN_DOMAINS$suffix"
    _debug2 DEPLOY_CONVERSANT_CDN_DOMAINS$suffix "$TMP_VAL"
    if [ -z "$TMP_VAL" ]; then
      TMP_VAL=$_cdomain
    fi
    _savedeployconf DEPLOY_CONVERSANT_CDN_DOMAINS$suffix "$TMP_VAL"
    #DEPLOY_CONVERSANT_CDN_DOMAINS="$TMP_VAL"
    _adomains="$TMP_VAL"

    _debug $DEPLOY_CONVERSANT_CDN_ID
    _debug $DEPLOY_CONVERSANT_CDN_SECRET
    _debug $_adomains
  
    _conversant_cdn_deploy

    _err_code="$?"
    if [ "$_err_code" -ne 0 ];then
      return $_err_code
    fi
  done

  return $_err_code
}

_conversant_cdn_deploy() {

  cmd="$DEFAULT_INSTALL_HOME/deploy/conversant_deploy.py"                  
  python3 $cmd -i $DEPLOY_CONVERSANT_CDN_ID -a $DEPLOY_CONVERSANT_CDN_SECRET \
  -d "$_adomains" -c "$_cfullchain" -k "$_ckey"

  _err_code="$?"
  return $_err_code

}



